﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using Eb;
using Ec;

public class EcEngineListener : IEcEngineListener
{
    //-------------------------------------------------------------------------
    public void init(EntityMgr entity_mgr, Entity et_node)
    {
        entity_mgr.regComponent<ClientApp<DefApp>>();

        entity_mgr.regEntityDef<EtApp>();

        entity_mgr.createEntity<EtApp>(null, et_node);
    }

    //-------------------------------------------------------------------------
    public void release()
    {
    }
}

class Program
{
    //-------------------------------------------------------------------------
    static EcEngine mEngine;
    static Stopwatch mStopwatch = new Stopwatch();
    const float mTimeLogicGap = 50.0f;// 毫秒

    //---------------------------------------------------------------------
    static void Main(string[] args)
    {
        Console.Title = "HelloWorldClient";
        Console.ForegroundColor = ConsoleColor.Green;
        EbLog.NoteCallback = Console.WriteLine;
        EbLog.WarningCallback = Console.WriteLine;
        EbLog.ErrorCallback = Console.WriteLine;

        EcEngineSettings settings;
        settings.ProjectName = "HelloWorld";
        settings.RootEntityType = "EtNode";
        settings.EnableComponentUCenter = false;
        mEngine = new EcEngine(ref settings, new EcEngineListener());

        EbLog.Note("按任意键退出。。。");

        float elapsed_tm = 0f;
        float watch_time = 0f;
        while (true)
        {
            mStopwatch.Reset();
            mStopwatch.Start();

            // 每帧更新
            try
            {
                mEngine.update(elapsed_tm);
            }
            catch (Exception ec)
            {
                EbLog.Error("Program.Main() Exception: " + ec);
            }

            mStopwatch.Stop();
            watch_time = mStopwatch.ElapsedMilliseconds;
            if (watch_time > mTimeLogicGap)
            {
                elapsed_tm = watch_time / 1000.0f;
            }
            else
            {
                Thread.Sleep((int)(mTimeLogicGap - watch_time));
                elapsed_tm = mTimeLogicGap / 1000.0f;
            }

            if (Console.KeyAvailable)
            {
                break;
            }
        }

        // 销毁
        if (mEngine != null)
        {
            mEngine.close();
            mEngine = null;
        }

        EbLog.Note("按任意键退出。。。");
        Console.ReadLine();
    }
}
